﻿using AntdUI;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestPressure
{
    public partial class DeviceAddForm : BaseForm
    {
        #region 属性

        private Device obj = new Device();

        #endregion

        #region 自定义方法

        /// <summary>重载
        /// 
        /// </summary>
        private void Reload()
        {
            DeviceNumberInput.Text = DateTimeOffset.Now.ToUnixTimeSeconds().ToString();
            DeviceNameInput.Text = "";
            DeviceAddressInput.Text = "";
            PortAddressSelect.Text = "";
            BaudRateSelect.Text = "";
        }
        /// <summary>重复检测
        /// 
        /// </summary>
        /// <returns></returns>
        private bool CheckRepeat()
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["device_name"] = DeviceNameInput.Text;
            List<Device> list = SqlSugarHelper.Inquireable<Device>(where);
            int count = list.Count;
            if (count > 0)
            {
                string msg = string.Format("【{0}】设备已存在", where["device_name"]);
                Error(msg, this);
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region 系统方法
        public DeviceAddForm()
        {
            InitializeComponent();
            Header.Text = "添加设备";
        }

        private void DeviceAddForm_Load(object sender, EventArgs e)
        {
            Reload();
        }
        #endregion

        #region 响应事件
        private void ConfirmButton_Click(object sender, EventArgs e)
        {
            if (DeviceNameInput.Text.Length == 0)
            {
                Error("请输入设备名称", this);
                DeviceNameInput.Focus();
                return;
            }
            obj.device_name = DeviceNameInput.Text;

            if (DeviceAddressInput.Text.Length == 0)
            {
                Error("请输入设备地址", this);
                DeviceAddressInput.Focus();
                return;
            }
            obj.device_address = DeviceAddressInput.Text.ObjToInt();

            if (PortAddressSelect.Text.Length == 0)
            {
                Error("请选择串口地址", this);
                PortAddressSelect.Focus();
                return;
            }
            obj.port_address = PortAddressSelect.Text;

            if (BaudRateSelect.Text.Length == 0)
            {
                Error("请选择波特率" , this);
                BaudRateSelect.Focus();
                return;
            }
            obj.baudrate = BaudRateSelect.Text.ObjToInt();
            obj.device_number = DeviceNumberInput.Text.ObjToInt();
            bool repeatRes = CheckRepeat();
            if (repeatRes)
            {
                return;
            }
            List<Device> list = new List<Device>();
            list.Add(obj);
            int res = SqlSugarHelper.Insertable(list);
            if (res > 0)
            {
                if (this.Owner is not null)
                {
                    DeviceManageForm frm = (DeviceManageForm)this.Owner;
                    frm.LoadList();
                    string msg = string.Format("【{0}】设备添加成功",obj.device_name);
                    frm.Success(msg, frm);
                }
                this.Close();
            }
            else
            {
                Error("添加设备失败", this);
            }
        }
        #endregion
    }
}
